package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.service.DownloadChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by slong.guo on 2017/5/5.
 */
@RestController
@RequestMapping(value = "/api/user/download")
public class DownloadChannelController {
    private static final Logger logger = LoggerFactory.getLogger(DownloadChannelController.class);

    @Autowired
    private DownloadChannelService channelService;


    @RequestMapping(value = "/channel", method = RequestMethod.POST)
    public APIResponse getChannel(@RequestBody Map map, HttpServletRequest request) {
        Integer cityId = (Integer) map.get("cityId");
        if (cityId == null || cityId.compareTo(0) < 0) {
            cityId = 0;
        }
        Integer paramUid = (Integer) map.get("uid");

        paramUid = (paramUid == null || paramUid.compareTo(0) < 0) ? 0 : paramUid;

        Integer uid = (Integer) request.getAttribute("uid");
        uid = (uid == null || uid.equals(0)) ? paramUid : uid;

        Integer dev = (Integer) request.getAttribute("dev");
        dev = dev == null ? 0 : dev;

        Object pt = map.get("platformType");
        Integer platformType = null;
        if (pt instanceof Integer) {
            platformType = (Integer) pt;
        } else if (pt instanceof String) {
            try {
                platformType = Integer.valueOf((String) pt);
            } catch (Exception e) {
                platformType = null;
            }
        }

        if (platformType != null) {
            if (platformType.equals(2)) {
                dev = 1;
            } else if (platformType.equals(1)) {
                dev = 2;
            }
        }

        return channelService.getChannelInfo(uid, cityId, dev);

    }
}
